package cn.zhuangcloud.newversion.register;

import cn.zhuangcloud.newversion.common.enums.RetCodeEnum;
import cn.zhuangcloud.newversion.common.enums.en.RetMessageEnum;
import cn.zhuangcloud.newversion.common.kit.PasswordKit;
import cn.zhuangcloud.newversion.common.model.Users;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class RegService {

    static final RegService me = new RegService();
    private static final Users userDao = new Users().dao();
    private static final Logger log = LoggerFactory.getLogger(RegService.class);

    Ret reg(String username, String password) {

        Users user = userDao.findFirst(userDao.getSqlPara("user.findByUsername", username));

        if (null != user) {
            return Ret.by("status",false).set("code", RetCodeEnum.USER_EXISTED).set("message", RetMessageEnum.USER_EXISTED);
        }

        String salt = StrKit.getRandomUUID();
        String pwd = PasswordKit.hash(password, salt);

        user = new Users()
                .setUsername(username)
                .setSalt(salt)
                .setPwd(pwd);

        if (!user.save()) {
            return Ret.by("status",false).set("code", RetCodeEnum.REG_FAIL).set("message", RetMessageEnum.REG_FAIL);
        }

        return Ret.by("status",true).set("code", RetCodeEnum.REG_SUCCESS).set("message", RetMessageEnum.REG_SUCCESS);
    }
}
